﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Threading;


namespace UDP_WPF
{
    public partial class Form1 : Form
    {
        private string _sendip;
        private int _sendport;
        private string _sendmsg;
        private string _recmsg;
        private int _recport;
        public Thread thread;
        public Form1()
        {
            InitializeComponent();
            recport.Text = "8888";
            sendipaddr.Text = "192.168.1.116:8888";
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.Dispose(true);
            this.Close();
            Application.Exit();
        }

        //服务器控制
        private void button2_Click(object sender, EventArgs e)
        {
            string port = recport.Text;
            if (port == "")
            {
                MessageBox.Show("端口不能为空");
                return;
            }
            else
            {
                _recport = Convert.ToInt32(recport.Text);
                Udp udp = new Udp(this);
                udp.rec(_recport);

            }
        }

        //发送
        private void button1_Click(object sender, EventArgs e)
        {
            string ipaddress = sendipaddr.Text;   //目标IP地址
            string msg = sendmsg.Text;   //发送的消息

            if ((ipaddress == "") || (msg == ""))
            {
                MessageBox.Show("错误：目标IP或消息内容为空");
                return;
            }
            else
            {
                Udp udp = new Udp(this);
                if (ipaddress.IndexOf(':') == -1)   //不填写端口号   192.168.1.116
                {
                    _sendip = ipaddress;
                    udp.send(_sendip, -1, msg);
                }
                else    //填写端口号   192.168.1.116:9999
                {
                    string[] str = ipaddress.Split(':');
                    _sendip = str[0];                     //IP
                    _sendport = Convert.ToInt32(str[1]); //port
                    for (int i = 0; i < 500; i++)
                        udp.send(_sendip, _sendport, "" + i);
                }

            }
        }

        //窗口置顶按钮
        private void Top_Click(object sender, EventArgs e)
        {
            if (istop.Checked)
            {
                this.TopMost = true;
            }
            else
            {
                this.TopMost = false;
            }
        }



        //清空数据
        private void button1_Click_1(object sender, EventArgs e)
        {
            //Keyevent.Instance().GetWindows(this.Handle,"");
            //recmsg.Focus();
            //thread = new Thread(test);
            //thread.Start();
            DataListener DataListener = new DataListener();
            DataListener.rec();
            recmsg.Text = "";
        }
        public void test()
        {
          
            //while (true)
            //{

            //    for (int i = 0; i < 20; i++)
            //    {

            //        Keyevent.Instance().KeyClick((byte)VrtualKey.VK_A);
            //    }
            //    Thread.Sleep(1000);
            //}
        }
    }
}
